package org.oschina.helper.email.impl;

import org.apache.commons.mail.EmailException;
import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.helper.email.IEMailHelper;
import org.oschina.helper.messagequeue.IMessageQueueHelper;
import org.oschina.queue.BaseQueue;
import org.oschina.util.EMailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 邮件实现类
 */
@Component
public class EMailHelperImpl implements IEMailHelper {

	@Autowired
	private IMessageQueueHelper messageQueueHelper;
	@Autowired
	private ConfigService configService;
	
	@Override
	public boolean send(String title, String content, String target)
	{
		return send(title,content,_getSystemHost(),_getSystemUsername(),_getSystemPassword(),target);
	}

	@Override
	public boolean send(String title, String content, String host, String from, String password, String target)
	{
		EmailSendBean emailSendBean=new EmailSendBean();
		emailSendBean.setTitle(title);
		emailSendBean.setContent(content);
		emailSendBean.setHost(host);
		emailSendBean.setFrom(from);
		emailSendBean.setPassword(password);
		emailSendBean.setTarget(target);
		messageQueueHelper.send(Constants.QUEUE_EMAIL_SEND_QUEUE, emailSendBean);
		return true;
	}
	
	private String _getSystemHost()
	{
		return (String) configService.getValueById(Config.CONFIG_26,String.class);
	}
	
	private String _getSystemUsername()
	{
		return (String) configService.getValueById(Config.CONFIG_27,String.class);
	}
	
	private String _getSystemPassword()
	{
		return (String) configService.getValueById(Config.CONFIG_28,String.class);
	}
}

/**
 * 邮件发送队列
 */
@Component
class EMailSendQueue extends BaseQueue
{
	private final static Logger logger=LoggerFactory.getLogger(EMailSendQueue.class);
	
	@Scheduled(fixedDelay=1000)
	private synchronized void handle()
	{
		EmailSendBean bean=(EmailSendBean) get(Constants.QUEUE_EMAIL_SEND_QUEUE, EmailSendBean.class);
		if(bean==null)
			return;
		
		try
		{
			EMailUtil.send(bean.getHost(), bean.getFrom(), bean.getPassword(), bean.getTarget(), bean.getTitle(), bean.getContent());
		}
		catch (EmailException e)
		{
			logger.error("",e);
		}
	}
}

class EmailSendBean
{
	private String title;
	private String content;
	private String host;
	private String from;
	private String password;
	private String target;
	
	public String getTitle() {
		return title;
	}
	public String getContent() {
		return content;
	}
	public String getHost() {
		return host;
	}
	public String getFrom() {
		return from;
	}
	public String getPassword() {
		return password;
	}
	public String getTarget() {
		return target;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setTarget(String target) {
		this.target = target;
	}
}